package zd.cornermemory.ui;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;

import zd.cornermemory.R;
import zd.cornermemory.bean.CornerCodeAnswerBean;
import zd.cornermemory.bean.CornerCodeReviewBean;
import zd.cornermemory.bean.CornerPicAnswerBean;
import zd.cornermemory.bean.Cube;
import zd.cornermemory.utils.ClickUtils;
import zd.cornermemory.utils.TimerForCode;

/**
 * 棱编码复习一
 */
public class EdgeCodeFx1Activity extends BaseCodeActivity {
    public static Map<String, Integer> pic = new HashMap<>();
    private ImageView img1;
    private ImageView img2;
    private ImageView img3;
    private ImageView img4;
    private View view1;
    private View view2;
    private View view3;
    private View view4;
    private TextView code;

    private List<CornerCodeReviewBean> cornerCodeReviewBeen;//题目集合
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_edge_code_fx1);
        setBackImage();
        img1 = findViewById(R.id.img1);
        img2 = findViewById(R.id.img2);
        img3 = findViewById(R.id.img3);
        img4 = findViewById(R.id.img4);
        view1 = findViewById(R.id.v1);
        view2 = findViewById(R.id.v2);
        view3 = findViewById(R.id.v3);
        view4 = findViewById(R.id.v4);
        code = findViewById(R.id.code);
        TimerForCode.state = 3;
        timerForCode.count2();
        setErrorBg(view1);
        setErrorBg(view2);
        setErrorBg(view3);
        setErrorBg(view4);
        initCornerCode();
        initCode();
        show();
        view1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (!ClickUtils.isFastDoubleClick()) {
                    boolean istrue = (boolean) view1.getTag();
                    if (istrue) {
                        mHandler.sendEmptyMessageDelayed(0, 200);
                    }
                }
            }
        });
        view2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (!ClickUtils.isFastDoubleClick()) {
                    boolean istrue = (boolean) view2.getTag();
                    if (istrue) {
                        mHandler.sendEmptyMessageDelayed(0, 200);
                    }
                }

            }
        });
        view3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (!ClickUtils.isFastDoubleClick()) {
                    boolean istrue = (boolean) view3.getTag();
                    if (istrue) {
                        mHandler.sendEmptyMessageDelayed(0, 200);
                    }
                }

            }
        });
        view4.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (!ClickUtils.isFastDoubleClick()) {
                    boolean istrue = (boolean) view4.getTag();
                    if (istrue) {
                        mHandler.sendEmptyMessageDelayed(0, 200);
                    }
                }

            }
        });

    }


    private Handler mHandler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            initCode();
            show();
        }
    };


    private void show(){
        CornerCodeAnswerBean cornerCodeAnswerBean = answerBeen.get(0);
        String codeStr = cornerCodeAnswerBean.getCode();
        code.setText(codeStr);
        List<Integer> has = new ArrayList<>();
        ImageView img;
        View v;
        for(int i = 0;i<4;i++) {
            int random = getRandom(4, has);
            has.add(random);
            CornerPicAnswerBean cornerPicAnswerBean = cornerCodeAnswerBean.getCornerPicAnswerBeen().get(random);
            boolean isTrue = cornerPicAnswerBean.isTrue();

            if (i == 0) {
                img = img1;
                v = view1;
            } else if (i == 1) {
                img = img2;
                v = view2;
            } else if (i == 2) {
                img = img3;
                v = view3;
            }else{
                img = img4;
                v = view4;
            }
            v.setTag(isTrue);
            img.setImageResource(cornerPicAnswerBean.getCornerPic());
            if(isTrue){
                setRightBg(v);
            }else{
                setErrorBg(v);
            }

        }


    }




    List<CornerCodeAnswerBean> answerBeen = new ArrayList<>();
    private void initCode(){
        answerBeen.clear();
        List<Integer> has = new ArrayList<>();
        Random r = new Random();
        int random = getRandom(12, has);
        has.add(random);
        CornerCodeReviewBean cornerCodeReviewBean = cornerCodeReviewBeen.get(random);
        String code = cornerCodeReviewBean.getCode();

        List<Integer> has2 = new ArrayList<>();
        List<CornerPicAnswerBean> cornerPicAnswerBeen = new ArrayList<>();

        cornerPicAnswerBeen.add(new CornerPicAnswerBean(cornerCodeReviewBean.getCornerPic().get(r.nextInt(2)),true));
        has2.add(random);



        int rr = getRandom(12, has2);
        cornerPicAnswerBeen.add(new CornerPicAnswerBean(cornerCodeReviewBeen.get(rr).getCornerPic().get(r.nextInt(2)),false));
        has2.add(rr);

        int rr2 = getRandom(12, has2);
        cornerPicAnswerBeen.add(new CornerPicAnswerBean(cornerCodeReviewBeen.get(rr2).getCornerPic().get(r.nextInt(2)),false));
        has2.add(rr2);

        int rr3 = getRandom(12, has2);
        cornerPicAnswerBeen.add(new CornerPicAnswerBean(cornerCodeReviewBeen.get(rr3).getCornerPic().get(r.nextInt(2)),false));


        answerBeen.add(new CornerCodeAnswerBean(code,cornerPicAnswerBeen));



    }


    @Override
    public void setTimerText(String str) {

    }

    private void initCornerCode(){
        Cube cube = new Cube();
        cube.init();
        cornerCodeReviewBeen = new ArrayList<>();
        cornerCodeReviewBeen.add(new CornerCodeReviewBean(getCodeStr(cube.getEdgeLocation0()),getEdgePic(0)));
        cornerCodeReviewBeen.add(new CornerCodeReviewBean(getCodeStr(cube.getEdgeLocation1()),getEdgePic(1)));
        cornerCodeReviewBeen.add(new CornerCodeReviewBean(getCodeStr(cube.getEdgeLocation2()),getEdgePic(2)));
        cornerCodeReviewBeen.add(new CornerCodeReviewBean(getCodeStr(cube.getEdgeLocation3()),getEdgePic(3)));
        cornerCodeReviewBeen.add(new CornerCodeReviewBean(getCodeStr(cube.getEdgeLocation4()),getEdgePic(4)));
        cornerCodeReviewBeen.add(new CornerCodeReviewBean(getCodeStr(cube.getEdgeLocation5()),getEdgePic(5)));
        cornerCodeReviewBeen.add(new CornerCodeReviewBean(getCodeStr(cube.getEdgeLocation6()),getEdgePic(6)));
        cornerCodeReviewBeen.add(new CornerCodeReviewBean(getCodeStr(cube.getEdgeLocation7()),getEdgePic(7)));
        cornerCodeReviewBeen.add(new CornerCodeReviewBean(getCodeStr(cube.getEdgeLocation8()),getEdgePic(8)));
        cornerCodeReviewBeen.add(new CornerCodeReviewBean(getCodeStr(cube.getEdgeLocation9()),getEdgePic(9)));
        cornerCodeReviewBeen.add(new CornerCodeReviewBean(getCodeStr(cube.getEdgeLocation10()),getEdgePic(10)));
        cornerCodeReviewBeen.add(new CornerCodeReviewBean(getCodeStr(cube.getEdgeLocation11()),getEdgePic(11)));

    }

    private String getCodeStr(List<String> list){
        String str = "";
        for (String s : list) {
            str += s;
        }
        return str;
    }







}
